package com.yangye.pisystem.controller;

import com.yangye.picommon.model.PiResult;
import com.yangye.pisystem.domain.SysMenu;
import com.yangye.pisystem.dto.SysMenuDTO;
import com.yangye.pisystem.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yangye
 * @date 2019/8/6
 */
@RestController
@RequestMapping("/sys/menus")
public class SysMenuController {

	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 新增
	 * @param sysMenu
	 * @return
	 */
	@PostMapping("/")
	@PreAuthorize("hasAuthority('sys:menu:add')")
	PiResult add(@RequestBody SysMenu sysMenu) {
		return PiResult.succeed(sysMenuService.add(sysMenu));
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	@PreAuthorize("hasAuthority('sys:menu:remove')")
	PiResult remove(@PathVariable Long id) {
		sysMenuService.remove(id);
		return PiResult.succeed();
	}

	/**
	 * 修改
	 * @param sysMenu
	 * @return
	 */
	@PutMapping("/")
	@PreAuthorize("hasAuthority('sys:menu:update')")
	PiResult update(@RequestBody SysMenu sysMenu) {
		sysMenuService.update(sysMenu);
		return PiResult.succeed();
	}

	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@PreAuthorize("hasAuthority('sys:menu:view')")
	PiResult get(@PathVariable Long id) {
		return PiResult.succeed(sysMenuService.findById(id));
	}

	/**
	 * 菜单列表
	 * @return
	 */
	@GetMapping("/tree")
	@PreAuthorize("hasAuthority('sys:menu')")
	PiResult listTree() {
		return PiResult.succeed(sysMenuService.listTree());
	}

	/**
	 * 当前用户授权的菜单
	 * @param token
	 * @return
	 */
	@GetMapping("/tree/user")
	PiResult listTreeByUser(@RequestHeader("Authorization") String token) {
		List<SysMenuDTO> tree = sysMenuService.listTreeByCurrentUser(token);
		return PiResult.succeed(tree);
	}
}
